package com.yyfax.ph.common.imageselector.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.yyfax.ph.R;
import com.yyfax.ph.common.imageselector.bean.Folder;

import java.util.ArrayList;
import java.util.List;

/***
 * Title: FolderAdapter
 * Description:
 * Company: YYLending(YouJingPuHui)
 *
 * @author XiongYuFeng
 *         date 2016年9月26日 下午4:23:06
 */
public class FolderAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater mInflater;

	private List<Folder> mFolders = new ArrayList<>();

	private int mImageSize;

	private int lastSelected = 0;

	private RequestManager mRequestManager;

	public FolderAdapter(Context context) {
		mContext = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mImageSize = mContext.getResources().getDimensionPixelOffset(R.dimen.folder_cover_size);
		mRequestManager = Glide.with(context);
	}

	/**
	 * 设置数据集
	 *
	 * @param folders 选择的文件夹
	 */
	public void setData(List<Folder> folders) {
		if (folders != null && folders.size() > 0) {
			mFolders = folders;
		} else {
			mFolders.clear();
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mFolders.size() + 1;
	}

	@Override
	public Folder getItem(int i) {
		if (i == 0)
			return null;
		return mFolders.get(i - 1);
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		ViewHolder holder;
		if (view == null) {
			view = mInflater.inflate(R.layout.mis_view_list_item_folder, viewGroup, false);
			holder = new ViewHolder(view);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		if (holder != null) {
			if (i == 0) {
				holder.name.setText("所有图片");
				holder.size.setText(mContext.getString(R.string.images_str, getTotalImageSize()));
				if (mFolders.size() > 0) {
					Folder f = mFolders.get(0);
					mRequestManager.load(f.cover.path).error(R.drawable.mis_skin_icon_default_error).centerCrop().into(holder.cover);
				}
			} else {
				holder.bindData(getItem(i));
			}
			if (lastSelected == i) {
				holder.indicator.setVisibility(View.VISIBLE);
			} else {
				holder.indicator.setVisibility(View.GONE);
			}
		}
		return view;
	}

	private int getTotalImageSize() {
		int result = 0;
		if (mFolders != null && mFolders.size() > 0) {
			for (Folder f : mFolders) {
				result += f.images.size();
			}
		}
		return result;
	}

	public void setSelectIndex(int i) {
		if (lastSelected == i)
			return;

		lastSelected = i;
		notifyDataSetChanged();
	}

	public int getSelectIndex() {
		return lastSelected;
	}

	class ViewHolder {
		@BindView(R.id.cover)
		ImageView cover;
		@BindView(R.id.name)
		TextView name;
		@BindView(R.id.size)
		TextView size;
		@BindView(R.id.indicator)
		ImageView indicator;

		ViewHolder(View view) {
			ButterKnife.bind(this, view);
			view.setTag(this);
		}

		void bindData(Folder data) {
			name.setText(data.name);
			size.setText(mContext.getString(R.string.images_str, data.images.size()));
			// 显示图片
			mRequestManager.load(data.cover.path).error(R.drawable.mis_skin_icon_default_error).centerCrop().into(cover);
		}
	}
}
